using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Runtime.InteropServices;

namespace MKDS_Course_Editor.NKM
{
    public class NkmPoint
    {
        public NkmPoint(float X, float Y, float Z, Color MarkerColor)
        {
            this.X = X;
            this.Y = Y;
            this.Z = Z;
            this.MarkerColor = MarkerColor;
        }
        public void Render(PaintEventArgs e, float ScaleX, float ScaleY)
        {
            e.Graphics.FillEllipse(new SolidBrush(MarkerColor), X * ScaleX - (5f / 2f) + e.ClipRectangle.Width / 2, Z * ScaleY - (5f / 2f) + e.ClipRectangle.Height / 2, 5, 5);
        }
        public float GetMinX(float ScaleX, int Width)
        {
            return (X * ScaleX) - (5f / 2f) + (Width / 2f);
        }
        public float GetMaxX(float ScaleX, int Width)
        {
            return (X * ScaleX) + (5f / 2f) + (Width / 2f);
        }
        public float GetMinY(float ScaleY, int Height)
        {
            return (Z * ScaleY) - (5f / 2f) + (Height / 2f);
        }
        public float GetMaxY(float ScaleY, int Height)
        {
            return (Z * ScaleY) + (5f / 2f) + (Height / 2f);
        }
        private Color marker;
        [Category("Marker")]
        public Color MarkerColor
        {
            get { return marker; }
            set { marker = value; }
        }
        private Bitmap RotateImg(Bitmap b, float angle)
        {
           /* //create a new empty bitmap to hold rotated image
            Bitmap returnBitmap = new Bitmap(b.Width, b.Height);
            if (b.Width != b.Height)
            {
                returnBitmap = new Bitmap((b.Width > b.Height ? b.Width : b.Height) * 2, (b.Width > b.Height ? b.Width : b.Height) * 2);
            }
            //make a graphics object from the empty bitmap
            Graphics g = Graphics.FromImage(returnBitmap);
            //move rotation point to center of image
            //g.TranslateTransform(returnBitmap.Width / 2f, returnBitmap.Height / 2f);
            //rotate
            g.RotateTransform(angle);
            //move image back
            //g.TranslateTransform(-returnBitmap.Width / 2f, -returnBitmap.Height / 2f);
            //draw passed in image onto graphics object
            g.DrawImage(b, new Point(0, 0));
            return returnBitmap;*/
            return (Bitmap)ImageTools.RotateImage(b, angle);
        }

        Bitmap TrimBitmap(Bitmap source)
        {
            Rectangle srcRect = default(Rectangle);
            BitmapData data = null;
            try
            {
                data = source.LockBits(new Rectangle(0, 0, source.Width, source.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
                byte[] buffer = new byte[data.Height * data.Stride];
                Marshal.Copy(data.Scan0, buffer, 0, buffer.Length);

                int xMin = int.MaxValue,
                    xMax = int.MinValue,
                    yMin = int.MaxValue,
                    yMax = int.MinValue;

                bool foundPixel = false;

                // Find xMin 
                for (int x = 0; x < data.Width; x++)
                {
                    bool stop = false;
                    for (int y = 0; y < data.Height; y++)
                    {
                        byte alpha = buffer[y * data.Stride + 4 * x + 3];
                        if (alpha != 0)
                        {
                            xMin = x;
                            stop = true;
                            foundPixel = true;
                            break;
                        }
                    }
                    if (stop)
                        break;
                }

                // Image is empty... 
                if (!foundPixel)
                    return null;

                // Find yMin 
                for (int y = 0; y < data.Height; y++)
                {
                    bool stop = false;
                    for (int x = xMin; x < data.Width; x++)
                    {
                        byte alpha = buffer[y * data.Stride + 4 * x + 3];
                        if (alpha != 0)
                        {
                            yMin = y;
                            stop = true;
                            break;
                        }
                    }
                    if (stop)
                        break;
                }

                // Find xMax 
                for (int x = data.Width - 1; x >= xMin; x--)
                {
                    bool stop = false;
                    for (int y = yMin; y < data.Height; y++)
                    {
                        byte alpha = buffer[y * data.Stride + 4 * x + 3];
                        if (alpha != 0)
                        {
                            xMax = x;
                            stop = true;
                            break;
                        }
                    }
                    if (stop)
                        break;
                }

                // Find yMax 
                for (int y = data.Height - 1; y >= yMin; y--)
                {
                    bool stop = false;
                    for (int x = xMin; x <= xMax; x++)
                    {
                        byte alpha = buffer[y * data.Stride + 4 * x + 3];
                        if (alpha != 0)
                        {
                            yMax = y;
                            stop = true;
                            break;
                        }
                    }
                    if (stop)
                        break;
                }

                srcRect = Rectangle.FromLTRB(xMin, yMin, xMax+1, yMax+1);
            }
            finally
            {
                if (data != null)
                    source.UnlockBits(data);
            }

            Bitmap dest = new Bitmap(srcRect.Width, srcRect.Height);
            Rectangle destRect = new Rectangle(0, 0, srcRect.Width, srcRect.Height);
            using (Graphics graphics = Graphics.FromImage(dest))
            {
                graphics.DrawImage(source, destRect, srcRect, GraphicsUnit.Pixel);
            }
            return dest;
        } 

        private float x = 0;
        [Category("Position")]
        public float X
        {
            get { return x; }
            set { x = value; }
        }
        private float y = 0;
        [Category("Position")]
        public float Y
        {
            get { return y; }
            set { y = value; }
        }
        private float z = 0;
        [Category("Position")]
        public float Z
        {
            get { return z; }
            set { z = value; }
        }
    }
}
